package com.controller;

import cn.hutool.core.io.FileUtil;
import com.github.pagehelper.PageInfo;
import com.pojo.Repairs;
import com.service.RepairsService;
import com.service.UserService;
import com.vo.RepairsVo;
import com.vo.Result;
import com.vo.repairsStateVo;
import org.springframework.stereotype.Controller;
import org.springframework.util.FileCopyUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.util.*;

@Controller

@RequestMapping("/Repairs")
public class RepairsController {
    @Resource
    private RepairsService repairsService;
    @Resource
    private UserService userService;
    @RequestMapping("/toRepairs")
    public String toRepairs(RepairsVo repairsVo){
        return "/Maintenance/leaders";
    }
    @RequestMapping("/selectRepairs")
    @ResponseBody
    public Result selectRepairs(RepairsVo repairsVo){
        List<Repairs> repairsList = repairsService.selectReqairsByVo(repairsVo);
        return Result.success(repairsList);
    }
    /**
     * 2.多文件上传
     * @param attach
     * @param request
     * @return
     */
    @RequestMapping("/uploadMore")
    @ResponseBody
    public Result uploadMore(@RequestParam("attach") MultipartFile[] attach, HttpServletRequest request){

        Map<String,Object> map = new HashMap<String,Object>();
        List<String> list = new ArrayList<String>();
        //4.获取上传到服务器的文件夹的位置
        String folder = request.getServletContext().getRealPath("/attach");

        for (MultipartFile file : attach) {
            //1.获取上传的原始文件名
            String filename = file.getOriginalFilename();
            //2.截取文件名的后缀
            String suffix = FileUtil.getSuffix(filename);
            //3.使用UUID生成新的文件名与后缀名进行拼接
            String newFileName = UUID.randomUUID().toString().replace("-","") + "." + suffix;
            try {
                //5.将上传的文件拷贝到attach中
                FileCopyUtils.copy(file.getBytes(),new File(folder,newFileName));
                list.add("attach/"+newFileName);
            }catch (Exception e){
                e.printStackTrace();
            }
        }
        map.put("attachPath",list);
        return Result.success(map);
    }
    //修改数据库
    @RequestMapping("/updatePath")
    @ResponseBody
    public Result updatePath( Repairs repairs){
        try {

            repairsService.updateRepairs(repairs);
            return Result.success("上传成功");
        }catch (Exception e){
            e.printStackTrace();
            return Result.fail("上传失败");
        }
    }
    @RequestMapping("/addRepairs")
    @ResponseBody
    public Result addRepairs(Repairs repairs){
        try {
            int i = repairsService.addReqairs(repairs);
            return Result.success("报修成功");
        }catch (Exception e){
            e.printStackTrace();
            return Result.success("报修失败");
        }
    }
    @RequestMapping("/updateState")
    @ResponseBody
    public Result updateState(repairsStateVo repairsStateVo){
        try {
            int i = repairsService.updateState(repairsStateVo);
            return Result.success("成功");
        }catch (Exception e){
            e.printStackTrace();
            return Result.success("失败");
        }
    }
    @RequestMapping("/toRecording")
    public String toRecording(){
        return "/Maintenance/Recording";
    }

    @RequestMapping("/selectRepairing")
    @ResponseBody
    public  Result selectRepairing(RepairsVo repairsVo){
        PageInfo<Repairs> pageInfo = repairsService.selectRecording(repairsVo);
        return Result.success(pageInfo);
    }
    @RequestMapping("/toOwnerRepairs")
    public String toOwnerRepairs(){
        return "/ownerChange/OwnerRepairs";
    }
    @RequestMapping("/selectAllRepairs")
    @ResponseBody
    public Result selectAllRepairs(RepairsVo repairsVo){
        PageInfo<Repairs> pageInfo = repairsService.selectAllRepairs(repairsVo);
        return Result.success(pageInfo);
    }
    @RequestMapping("/toRepairJob")
    public String toRepairJob(){
        return "/ownerChange/RepairJob";
    }
    @RequestMapping("/selectAllRepairJob")
    @ResponseBody
    public  Result selectRepairJob(RepairsVo repairsVo ,int employee){
        repairsVo.setEmployeeid(employee);
        PageInfo<Repairs> pageInfo = repairsService.selectAllRepairsjob(repairsVo);
        return Result.success(pageInfo);
    }
    /**
     * 1.单文件上传
     * @param attach
     * @param request
     * @return
     */
    @RequestMapping("/uploads")
    @ResponseBody
    public Result uploads(@RequestParam("attach") MultipartFile attach, HttpServletRequest request){
        //1.获取上传的原始文件名
        String filename = attach.getOriginalFilename();
        //2.截取文件名的后缀
        String suffix = FileUtil.getSuffix(filename);
        //3.使用UUID生成新的文件名与后缀名进行拼接
        String newFileName = UUID.randomUUID().toString().replace("-","") + "." + suffix;
        //4.获取上传到服务器的文件夹的位置
        String folder = request.getServletContext().getRealPath("/attach");
        System.out.println(folder);
        try {
            //5.将上传的文件拷贝到attach中
            FileCopyUtils.copy(attach.getBytes(),new File(folder,newFileName));
            Map<String,Object> map = new HashMap<String,Object>();
            map.put("attachPath","attach/"+newFileName);
            return Result.success(map);
        }catch (Exception e){
            e.printStackTrace();
            return Result.success("上传失败");
        }
    }
    @RequestMapping("/updateRepairs")
    @ResponseBody
    public Result updateRepairs(Repairs repairs){
        try {
            int i = repairsService.updateRepairs(repairs);
            return Result.success("成功");
        }catch (Exception e){
            e.printStackTrace();
            return Result.success("失败");
        }
    }
    @RequestMapping("/updateEmployee")
    @ResponseBody
    public Result updateEmployee(repairsStateVo repairsStateVo){
        try {
            int i = repairsService.updateEmployee(repairsStateVo);
            return Result.success("退单成功");
        }catch (Exception e){
            e.printStackTrace();
            return Result.success("退单失败");
        }
    }
    @RequestMapping("/toAllRepairing")
    public String toAllRepairing(){
        return "/ownerChange/AllRepairing";
    }
    @RequestMapping("/selectAllRepairing")
    @ResponseBody
    public Result selectAllRepairing(RepairsVo repairsVo){
        PageInfo<Repairs> pageInfo = repairsService.selectAllRepairing(repairsVo);
        return Result.success(pageInfo);
    }
    @RequestMapping("/selectRepairsDetails")
    @ResponseBody
    public Result selectRepairsDetails(Integer id){
        Repairs repairs = repairsService.selectReparisDetails(id);
        return Result.success(repairs);
    }
    /*-------------------------------------*/
    @RequestMapping("/tojobRecording")
    public String tojobRecording(){
        return "/ownerChange/jobRecording";
    }
    @RequestMapping("/selectRepairsEleByFinish")
    @ResponseBody
    public Result selectRepairsEleByFinish(RepairsVo repairsVo,Integer employeeid){
        repairsVo.setEmployeeid(employeeid);
        PageInfo<Repairs> pageInfo = repairsService.selectReparisByElefinishi(repairsVo);
        return Result.success(pageInfo);
    }
}
